﻿// *********************************************************************
// [DCOM Productions]
// [Copyright (C) DCOM Productions All rights reserved.]
// *********************************************************************

namespace BitFlex.Windows.Forms {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Represents the Windows Explorer Labeled Divider
    /// </summary>
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(ExplorerLabelDivider), "Resources.ExplorerLabelDividerToolboxBitmap.bmp")]
    public partial class ExplorerLabelDivider : Control {
        /// <summary>
        /// Initializes a new instance of the BitFlex.Windows.Forms.ExplorerLabelDivider class
        /// </summary>
        public ExplorerLabelDivider() {
            InitializeComponent();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            // Setup Windows Vista+7 Style
            if (Environment.OSVersion.Version.Major >= 6) {
                Font = new Font("Segoe UI", 9.0F, GraphicsUnit.Point);
                ForeColor = Color.FromArgb(0, 51, 153);
                LineColor = Color.FromArgb(226, 226, 226);
            }
            else {
                ForeColor = Color.Black;
                LineColor = Color.Black;
            }
        }

        /// <summary>
        /// Gets or sets base.Font
        /// </summary>
        [Category("Appearance")]
        [DefaultValue("Segoe UI, 9pt")]
        [Description("Gets or sets base.Font")]
        new public Font Font {
            get {
                return base.Font;
            }
            set {
                base.Font = value;
                Invalidate();
            }
        }

        private Color m_LineColor;
        /// <summary>
        /// Gets or sets the color of the divider line
        /// </summary>
        [Category("Appearance")]
        [Description("Gets or sets the color of the divider line")]
        public Color LineColor {
            get {
                return m_LineColor;
            }
            set {
                m_LineColor = value;
                Invalidate();
            }
        }

        /// <summary>
        /// Gets or sets the control's text and causes the control to repaint
        /// </summary>
        [Category("Appearance")]
        [Description("Gets or sets the control's text and causes the control to repaint")]
        public override string Text {
            get {
                return base.Text;
            }
            set {
                base.Text = value;
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs pe) {
            Size size = pe.Graphics.MeasureString(Text, Font).ToSize();
            int x, y;
            x = -3;
            y = (ClientRectangle.Height / 2) - (size.Height / 2);
            using (SolidBrush brush = new SolidBrush(ForeColor)) {
                pe.Graphics.DrawString(Text, Font, brush, x, y);
            }
            using (SolidBrush brush = new SolidBrush(LineColor)) {
                Rectangle rect = new Rectangle();
                rect.X = x + size.Width + 4;
                rect.Y = (ClientRectangle.Height / 2) + 1;
                rect.Width = ClientRectangle.Width - size.Width - 1;
                rect.Height = 1;
                pe.Graphics.FillRectangle(brush, rect);
            }
        }
    }
}